<script lang="ts" setup>
import { reactive, ref, watch } from "vue"
import { createJobDataApi, updateJobDataApi, getJobDataApi } from "@/api/job"
import { type IGetJobData } from "@/api/job/types/job"
import { type FormInstance, type FormRules, ElMessage } from "element-plus"
import { RefreshRight } from "@element-plus/icons-vue"
import { usePagination } from "@/hooks/usePagination"
import { checkPhoneNumber } from "@/utils/validate"
defineOptions({
  name: "Job"
})

const loading = ref<boolean>(false)
const { paginationData } = usePagination()

//#region 增
const dialogVisible = ref<boolean>(false)
const formRef = ref<FormInstance | null>(null)
const formData = reactive({
  name: "", ///**账户名 */
  password: "", ///**密码 */
  phoneNumber: "", ///**登录手机号 */
  supAgent: "", // /**上级代理 */
  image: "",
  agentLeve: "" ///**分销等级 */
})
const formRules: FormRules = reactive({
  name: [{ required: true, trigger: "blur", message: "请输入账户名称" }],
  password: [{ required: true, trigger: "blur", message: "请输入密码" }],
  phoneNumber: [{ required: true, validator: checkPhoneNumber, trigger: "blur" }],
  supAgent: [{ required: true, trigger: "blur", message: "选择上级代理" }],
  agentLeve: [{ required: true, trigger: "blur", message: "请选择分销等级" }]
})
const handleCreate = () => {
  formRef.value?.validate((valid: boolean) => {
    if (valid) {
      uploadFuc()
    } else {
      return false
    }
  })
}
const resetForm = () => {
  currentUpdateId.value = undefined
  clearValidate()
}

//#region 改
const currentUpdateId = ref<undefined | string>(undefined)
//#endregion

//#region 查
const tableData = ref<IGetJobData[]>([])
const agentData = ref<any>([
  {
    id: 1,
    name: "无",
    leve: 0,
    children: []
  },
  {
    id: 2,
    name: "云邮号卡",
    leve: 0,
    children: [
      {
        id: 201,
        name: "号卡科技",
        leve: 1,
        children: [
          {
            id: 20101,
            name: "巨惠号卡",
            leve: 2
          }
        ]
      },
      {
        id: 202,
        name: "测试",
        leve: 1,
        children: []
      }
    ]
  }
])
const treeProps = reactive({
  children: "children",
  value: "name",
  label: "name"
})
const getTableData = () => {
  loading.value = true
  getJobDataApi({
    page: paginationData.currentPage,
    size: paginationData.pageSize
    // name: searchData.name || undefined,
    // phone: searchData.phone || undefined
  })
    .then((res: any) => {
      // paginationData.total = res.data.total
      tableData.value = res.data
    })
    .catch(() => {
      tableData.value = []
    })
    .finally(() => {
      loading.value = false
    })
}

const handleRefresh = () => {
  getTableData()
}
//#endregion

const uploadFuc = async () => {
  if (currentUpdateId.value === undefined) {
    createJobDataApi({
      name: formData.name,
      image: formData.image
    }).then(() => {
      ElMessage.success("新增成功")
      dialogVisible.value = false
      getTableData()
    })
  } else {
    updateJobDataApi({
      id: currentUpdateId.value,
      name: formData.name,
      image: formData.image
    }).then(() => {
      ElMessage.success("修改成功")
      dialogVisible.value = false
      getTableData()
    })
  }
}
/** 监听分页参数的变化 */
watch([() => paginationData.currentPage, () => paginationData.pageSize], getTableData, { immediate: true })
// ** 清除规则报错信息
const clearValidate = () => {
  formRef.value?.clearValidate()
  formRef.value?.resetFields()
}
</script>

<template>
  <div class="app-container">
    <!-- <el-card v-loading="loading" shadow="never" class="search-wrapper">
      <el-form ref="searchFormRef" :inline="true" :model="searchData">
        <el-form-item prop="name" label="账户名">
          <el-input v-model="searchData.name" placeholder="请输入" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :icon="Search" @click="handleSearch">查询</el-button>
          <el-button :icon="Refresh" @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card> -->
    <el-card v-loading="loading" shadow="never">
      <div class="toolbar-wrapper">
        <div>
          <!-- <el-button type="primary" :icon="CirclePlus" @click="dialogVisible = true">添加账户</el-button> -->
        </div>
        <div>
          <el-tooltip content="刷新表格">
            <el-button type="primary" :icon="RefreshRight" circle @click="handleRefresh" />
          </el-tooltip>
        </div>
      </div>
      <div class="table-wrapper">
        <el-table :data="tableData">
          <el-table-column type="selection" width="50" align="center" />
          <!-- <el-table-column prop="id" label="代理编号" align="center" /> -->
          <el-table-column type="index" label="代理编号" width="120" align="center">
            <template #default="{ $index }">
              {{ (paginationData.currentPage - 1) * paginationData.pageSize + $index + 1 }}
            </template>
          </el-table-column>
          <el-table-column prop="name" label="代理名称" align="center" />
          <el-table-column prop="supAgent" label="下级代理" align="center" />
          <el-table-column prop="agentLeve" label="分佣金额" align="center" />
          <el-table-column prop="" label="有效订单数量" align="center" />
          <el-table-column prop="" label="代理提成" align="center" />
          <el-table-column prop="" label="提现情况" align="center" />
          <!-- <el-table-column fixed="right" label="操作" width="150" align="center">
            <template #default="scope">
              <el-button type="primary" text bg size="small" @click="handleUpdate(scope.row)">修改</el-button>
              <el-button type="danger" text bg size="small" @click="handleDelete(scope.row)">删除</el-button>
            </template>
          </el-table-column> -->
        </el-table>
      </div>
    </el-card>
    <!-- 新增/修改 -->
    <el-dialog
      v-model="dialogVisible"
      :title="currentUpdateId === undefined ? '新增账户' : '修改账户'"
      @close="resetForm"
      width="60%"
    >
      <el-form ref="formRef" :model="formData" :rules="formRules" label-width="100px" label-position="left">
        <el-form-item prop="name" label="账户名">
          <el-input v-model="formData.name" placeholder="请输入" />
        </el-form-item>
        <el-form-item prop="password" label="密码">
          <el-input v-model="formData.password" placeholder="请输入" />
        </el-form-item>
        <el-form-item prop="phoneNumber" label="登录手机号">
          <el-input v-model="formData.phoneNumber" :maxlength="11" show-word-limit placeholder="请输入" />
        </el-form-item>
        <el-form-item prop="supAgent" label="上级代理">
          <!-- <el-select v-model="formData.supAgent" clearable placeholder="请选择" style="width: 240px">
            <el-option label="无" value="无" />
          </el-select> -->
          <el-tree-select
            v-model="formData.supAgent"
            :data="agentData"
            :props="treeProps"
            :check-strictly="true"
            filterable
          />
        </el-form-item>
        <el-form-item prop="agentLeve">
          <template #label>
            <span>分销等级</span>
            <el-tooltip class="box-item" effect="customized" placement="bottom">
              <template #content>
                月激活50张以内,【会员】,每张利润50-80元<br />
                月激活50张以上,【初级代理】,每张利润80-100元<br />
                月激活200张以上,【中级代理】,每张利润100-130<br />
                月激活500张以上,【高级代理】,每张利润130-160<br />
                月激活1000单以上,【核心代理】,每张利润160以上<br />
              </template>
              <el-icon style="cursor: pointer"><InfoFilled /></el-icon>
            </el-tooltip>
          </template>
          <el-select v-model="formData.agentLeve" clearable placeholder="请选择" style="width: 240px">
            <!-- <el-option v-for="item in comboSource" :key="item.id" :label="item.name" :value="item.name" /> -->
            <el-option label="会员" value="会员" />
            <el-option label="初级代理" value="初级代理" />
            <el-option label="中级代理" value="中级代理" />
            <el-option label="高级代理" value="高级代理" />
            <el-option label="核心代理" value="核心代理" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleCreate">确认</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<style lang="scss" scoped>
.search-wrapper {
  margin-bottom: 20px;
  :deep(.el-card__body) {
    padding-bottom: 2px;
  }
}

.toolbar-wrapper {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}

.table-wrapper {
  margin-bottom: 20px;
}

.pager-wrapper {
  display: flex;
  justify-content: flex-end;
}
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.block {
  padding: 30px 0;
  text-align: center;
  box-sizing: border-box;
  vertical-align: top;
}
</style>
<style>
.el-popper.is-customized {
  /* Set padding to ensure the height is 32px */
  padding: 6px 12px;
  background: linear-gradient(90deg, rgb(159, 229, 151), rgb(204, 229, 129));
}

.el-popper.is-customized .el-popper__arrow::before {
  background: linear-gradient(45deg, #b2e68d, #bce689);
  right: 0;
}
</style>
